package com.jgssp.ad.sdk.mediation.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.banner.MediationCustomBannerLoader;
import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomServiceConfig;
import com.jgssp.ad.sdk.mediation.utils.PriceUtil;
import com.jgssp.ad.sdk.mediation.utils.ThreadUtils;

import java.util.Map;

import cn.jiguang.jgssp.ad.ADJgBannerAd;
import cn.jiguang.jgssp.ad.data.ADJgAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgBannerAdListener;

/**
 * @Description: Banner广告适配器
 */
public class CustomerBannerAdapter extends MediationCustomBannerLoader {

    private FrameLayout flContainer;
    private ADJgBannerAd bannerAd;

    @Override
    public void load(final Context context, AdSlot gmAdSlotBanner, final MediationCustomServiceConfig gmCustomServiceConfig) {
        ThreadUtils.runOnUIThreadByThreadPool(new Runnable() {
            @Override
            public void run() {
                if (!(context instanceof Activity)) {
                    callLoadFail(-1, "JgSSP Context must be Activity");
                    return;
                }

                flContainer = new FrameLayout(context);
                flContainer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                bannerAd = new ADJgBannerAd((Activity) context, flContainer);
                bannerAd.setForceCheck(false);
                bannerAd.setListener(new ADJgBannerAdListener() {

                    @Override
                    public void onAdExpose(ADJgAdInfo adInfo) {
                        callBannerAdShow();
                    }

                    @Override
                    public void onAdClick(ADJgAdInfo adInfo) {
                        callBannerAdClick();
                    }

                    @Override
                    public void onAdClose(ADJgAdInfo adInfo) {
                        callBannerAdClosed();
                    }

                    @Override
                    public void onAdReceive(ADJgAdInfo adInfo) {
                        if (isClientBidding() && adInfo != null) {
                            //bidding广告成功回调，回传竞价广告价格 单位：分
                            callLoadSuccess(PriceUtil.toCent(adInfo.getECPM()));
                        } else {
                            //普通广告成功回调
                            callLoadSuccess();
                        }
                    }

                    @Override
                    public void onAdFailed(ADJgError error) {
                        callLoadFail(error.getCode(), error.getError());
                    }
                });
                if (gmCustomServiceConfig != null) {
                    bannerAd.loadAd(gmCustomServiceConfig.getADNNetworkSlotId());
                }
            }
        });
    }

    @Override
    public View getAdView() {
        return flContainer;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (bannerAd != null) {
            bannerAd.release();
            bannerAd = null;
        }
        flContainer = null;
    }

    @Override
    public void receiveBidResult(boolean win, double winnerPrice, int loseReason, Map<String, Object> extra) {
    }

    /**
     * 是否clientBidding广告
     *
     * @return
     */
    public boolean isClientBidding() {
        return getBiddingType() == MediationConstant.AD_TYPE_CLIENT_BIDING;
    }
}
